home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Libraries / DebugNew / DebugNew.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-28  |  3.7 KB  |  114 lines  |  [TEXT/MMCC]

  1. /*
  2.  *  DebugNew.h
  3.  *
  4.  *  Copyright © 1993 metrowerks inc.  All rights reserved.
  5.  * 
  6.  *    A debugging layer over the standard operator new and delete
  7.  *  to detect memory overwrites, incorrect deletes, and memory leaks.
  8.  *  See DebugNew.doc in the CodeWarrior Examples:Debug New demo: for 
  9.  *  background and usage instructions.
  10.  */
  11.  
  12. #ifndef DebugNew_H
  13. #define DebugNew_H
  14.  
  15. #include <stddef.h>
  16.  
  17. #define DEBUG_NEW_OFF        0    // disabled
  18. #define DEBUG_NEW_BASIC        1    // detect overwrites, heuristically detect bad deletes
  19. #define DEBUG_NEW_LEAKS        2    // all basic testing, plus memory leaks
  20.  
  21. #ifndef DEBUG_NEW                        // Default setting if not defined already. You can
  22. #define DEBUG_NEW    DEBUG_NEW_BASIC     // define DEBUG_NEW as 0,1 or 2 in your prefix or elsewhere.
  23. #endif
  24.  
  25. #if DEBUG_NEW == DEBUG_NEW_OFF
  26.   #define NEW new
  27.    
  28. #elif DEBUG_NEW == DEBUG_NEW_BASIC
  29.   #define NEW new
  30.    
  31. #elif DEBUG_NEW == DEBUG_NEW_LEAKS
  32.   #define NEW new(__FILE__, __LINE__)
  33.  
  34.   void* operator new(size_t size, const char*, int);  
  35.   
  36.               // Validate all allocated blocks, only available when leak checking is
  37.               // enabled.
  38.               
  39.   void  DebugNewValidateAllBlocks();
  40.  
  41. #else
  42. #error DEBUG_NEW has an undefined value
  43. #endif
  44.  
  45.     // Try to validate that a pointer points to a valid, uncorrupted block.
  46.     // Invokes error handler if validation fails. Will not work properly
  47.     // for C++ arrays. Does nothing when DebugNew is disabled
  48.     
  49. void DebugNewValidatePtr(void*);
  50.  
  51. #if DEBUG_NEW >= DEBUG_NEW_BASIC
  52.  
  53.     // this variable holds the number of active allocations
  54.     
  55. extern unsigned long gDebugNewAllocCount;
  56.  
  57.     // this variable holds the total #bytes currently allocated via operator new(),
  58.     // not including DebugNew overhead.
  59.     
  60. extern unsigned long gDebugNewAllocCurr;
  61.  
  62.     // this variable holds the maximum #bytes allocated at any given point in
  63.     // the program execution, not counting DebugNew overhead. Useful for
  64.     // memory tuning.
  65.     
  66. extern unsigned long gDebugNewAllocMax;
  67.  
  68.     // this variable holds flags that control run-time behavior
  69.     // of DebugNew. The available flags are defined in the 
  70.     // following enum (only one currently).
  71.     
  72. extern unsigned long gDebugNewFlags;
  73.  
  74. enum          // gDebugNewFlags bits, refer to DebugNew.doc for information
  75. {
  76.     dnDontFreeBlocks = 1    // don't release free blocks back to allocator,
  77.                             // provides extended checking if leak checking enabled
  78. };
  79.  
  80.  
  81.     // Call to memory leak tracking status to a file called
  82.     // "leaks.log" in the application directory
  83.     
  84. void DebugNewReportLeaks();
  85.  
  86.     // You can optionally provide an error handler to be called when
  87.     // errors are detected. The default routine just calls DebugStr.
  88.     // Call DebugNewSetErrorHandler to set handler, it returns the
  89.     // old handler.
  90.     
  91. typedef void (*DebugNewErrorHandler_t)(short);
  92.  
  93. DebugNewErrorHandler_t    DebugNewSetErrorHandler(DebugNewErrorHandler_t newHandler);
  94.  
  95. enum DebugNewError_t            // error codes passed to error handler
  96. {
  97.     dbgnewNullPtr,                // validate called with NULL pointer
  98.     dbgnewTooManyFrees,            // #deletes doesn't match #news, too many deletes
  99.     dbgnewPointerOutsideHeap,    // validate or free call for pointer outside application heap
  100.     dbgnewFreeBlock,            // validate or delete called for free block
  101.     dbgnewBadHeader,            // validate or delete called for block with bad header. 
  102.                                 // Bad ptr or overwritten header.
  103.     dbgnewBadTrailer,            // validate or delete called with block whose trailer was overwritten
  104.     dbgnewBlockNotInList,        // validate or delete called with block with valid header/trailer, 
  105.                                 // but block is not in block list (internal error?)
  106.     dbgnewFreeBlockOverwritten  // dnDontFreeBlocks was enabled, and a free block was
  107.                                 // modified. Usually indicates a write through a dangling pointer.
  108. };    
  109.  
  110. #endif  // DEBUG_NEW >= DEBUG_NEW_BASIC
  111.  
  112. #endif    // DebugNew_H
  113.  
  114.